package com.yitiao.base;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

import java.awt.*;
import java.util.HashMap;
import java.util.Map;

public class TestWord extends ApplicationFrame {

    Map<Integer,Integer> map = new HashMap<>();
    public TestWord(String titile, Map<Integer,Integer> map){
        //程序入口
        super(titile);
        this.map = map;
        //获取曲线数据，这里面是我自己造的一条
        CategoryDataset dataset=createDataset();
        //再用数据获取jfreechart对象，这里使用的是createLineChart折线图
        JFreeChart chart=createChart(dataset);
//        获取panel，可以用它来设置很多样式的参数，这里就先俺默认了
        ChartPanel chartPanel=new ChartPanel(chart);
        chartPanel.setPreferredSize(new Dimension(800,500));
        //这个是jfreechart内部的方法
        setContentPane(chartPanel);
    }
    private CategoryDataset createDataset(){
        DefaultCategoryDataset dataset=new DefaultCategoryDataset();
        map.forEach((k,v)->{
            dataset.addValue(v,"Letter",k);
        });
//        dataset.addValue(212,"Letter","A");
//        dataset.addValue(504,"Letter","B");
//        dataset.addValue(1520,"Letter","C");
//        dataset.addValue(1842,"Letter","D");
//        dataset.addValue(2991,"Letter","E");
        return dataset;
    }
    private JFreeChart createChart(CategoryDataset dataset){
        JFreeChart chart= ChartFactory.createLineChart(
                "Line TestWord Demo",
                "Category Axis",
                "Value Axis",
                dataset
        );
        return  chart;
    }

    public static void main(String args[])throws Exception{
        //程序执行从构造方法开始
        TestWord demo=new TestWord("曲线示例",new HashMap<>());
        //下面这些是弹框展示用的
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
    }



}
